package com.hjm.project.AgentApplyTailmoney.AgentApplyTailmoney.controller;

import java.util.List;
import java.util.Map;

import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.framework.config.CustomConfig;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.hjm.framework.aspectj.lang.annotation.Log;
import com.hjm.framework.aspectj.lang.enums.BusinessType;
import com.hjm.project.AgentApplyTailmoney.AgentApplyTailmoney.domain.AgentApplyTailmoney;
import com.hjm.project.AgentApplyTailmoney.AgentApplyTailmoney.service.IAgentApplyTailmoneyService;
import com.hjm.framework.web.controller.BaseController;
import com.hjm.framework.web.domain.AjaxResult;
import com.hjm.common.utils.poi.ExcelUtil;
import com.hjm.framework.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务商尾款充值抵扣Controller
 * 
 * @author AgentApplyTailmoney
 * @date 2020-11-27
 */
@Controller
@RequestMapping("/AgentApplyTailmoney/AgentApplyTailmoney")
public class AgentApplyTailmoneyController extends BaseController
{
    private String prefix = "AgentApplyTailmoney/AgentApplyTailmoney";

    @Autowired
    private IAgentApplyTailmoneyService agentApplyTailmoneyService;

    @RequiresPermissions("AgentApplyTailmoney:AgentApplyTailmoney:view")
    @GetMapping()
    public String AgentApplyTailmoney()
    {
        return prefix + "/AgentApplyTailmoney";
    }

    /**
     * 查询服务商尾款充值抵扣列表
     */
    @RequiresPermissions("AgentApplyTailmoney:AgentApplyTailmoney:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AgentApplyTailmoney agentApplyTailmoney)
    {
        startPage();
        List<AgentApplyTailmoney> list = agentApplyTailmoneyService.selectAgentApplyTailmoneyList(agentApplyTailmoney);
        return getDataTable(list);
    }

    /**
     * 导出服务商尾款充值抵扣列表
     */
    @RequiresPermissions("AgentApplyTailmoney:AgentApplyTailmoney:export")
    @Log(title = "服务商尾款充值抵扣", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AgentApplyTailmoney agentApplyTailmoney)
    {
        List<AgentApplyTailmoney> list = agentApplyTailmoneyService.selectAgentApplyTailmoneyList(agentApplyTailmoney);
        ExcelUtil<AgentApplyTailmoney> util = new ExcelUtil<AgentApplyTailmoney>(AgentApplyTailmoney.class);
        return util.exportExcel(list, "AgentApplyTailmoney");
    }

    /**
     * 新增服务商尾款充值抵扣
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存服务商尾款充值抵扣
     */
    @RequiresPermissions("AgentApplyTailmoney:AgentApplyTailmoney:add")
    @Log(title = "服务商尾款充值抵扣", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AgentApplyTailmoney agentApplyTailmoney)
    {
        return toAjax(agentApplyTailmoneyService.insertAgentApplyTailmoney(agentApplyTailmoney));
    }

    /**
     * 修改服务商尾款充值抵扣
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AgentApplyTailmoney agentApplyTailmoney = agentApplyTailmoneyService.selectAgentApplyTailmoneyById(id);
        agentApplyTailmoney.setFileid(CustomConfig.getImgUrl() + agentApplyTailmoney.getFileid());
        mmap.put("agentApplyTailmoney", agentApplyTailmoney);
        return prefix + "/edit";
    }

    /**
     * 修改保存服务商尾款充值抵扣
     */
    @RequiresPermissions("AgentApplyTailmoney:AgentApplyTailmoney:edit")
    @Log(title = "服务商尾款充值抵扣", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AgentApplyTailmoney agentApplyTailmoney)
    {
        return toAjax(agentApplyTailmoneyService.updateAgentApplyTailmoney(agentApplyTailmoney));
    }

    /**
     * 删除服务商尾款充值抵扣
     */
    @RequiresPermissions("AgentApplyTailmoney:AgentApplyTailmoney:remove")
    @Log(title = "服务商尾款充值抵扣", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(agentApplyTailmoneyService.deleteAgentApplyTailmoneyByIds(ids));
    }




    /**
     * 修改服务商尾款充值抵扣
     */
    @GetMapping("/auditView")
    public String auditView(ModelMap mmap,AgentApplyTailmoney agentApplyTailmoney)
    {
        AgentApplyTailmoney agentApplyTailmoney1 = agentApplyTailmoneyService.selectAgentApplyTailmoneyById(agentApplyTailmoney.getId());
        mmap.put("agentApplyTailmoney", agentApplyTailmoney1);
        return prefix + "/auditView";
    }


    /**
     * 查找带回页面
     */
    @GetMapping("/infoView")
    public String infoView() {
        return prefix + "/infoView";
    }

    @Log(title = "获取服务商尾款")
    @PostMapping("/findAgentPage")
    @ResponseBody
    public TableDataInfo findAgentPage(@RequestParam Map param){
        startPage();
        List<Map> agentPage = agentApplyTailmoneyService.findAgentPage(param);
        return getDataTable(agentPage);
    }
    @Log(title = "充值申请")
    @RequestMapping("saveApplyTMoney")
    @ResponseBody
    public  AjaxResult saveApplyTMoney(MultipartFile[] imgFile, AgentApplyTailmoney applyTailmoney) throws Exception {
        if (Double.parseDouble(applyTailmoney.getApplyMoney())>Double.parseDouble(applyTailmoney.getSurplusMoney())){
            return error("申请结算金额不得大于未结算金额！");
        }
        List<AgentApplyTailmoney> agentApplyTailmoney = agentApplyTailmoneyService.selectAgentApplyTailmoneyLists(applyTailmoney);
        if (CollectionUtils.isNotEmpty(agentApplyTailmoney)){
            return error("当前服务商已有申请，请处理后在进行操作！");
        }
        applyTailmoney.setAddMan(ShiroUtils.getSysUser().getLoginName());
        int result= agentApplyTailmoneyService.saveApplyTMoney(applyTailmoney,imgFile);
        return toAjax(result);
    }

}
